﻿using Domain.Model;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class TradeController : LoginedController
    {
        //
        // GET: /Trade/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult IndexPage(int pageIndex, int pageSize)
        {
            var list = DbContext.Publish.OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(list.TotalCount, this.ViewHtml(list));
        }
        public ActionResult Create()
        {
            var model = new Publish();
            if (DbContext.Publish.Count() > 0)
                model.No = DbContext.Publish.Select(i => i.No).Max() + 1;
            else
                model.No = 1;
            return View(model);
        }
        [HttpPost]
        public ActionResult Create(Publish model)
        {
            model.Total = model.Quantity * model.UnitPrice;
            DbContext.Publish.Add(model);
            DbContext.SaveChanges();
            return Json(new { state = true, value = string.Empty });
        }

        public void ExportExcel()
        {
            DataTable dt = this.DbContext.Database.SqlQuery("Select * from Publish", null);
            //ExcelHelp.ExportExcel(dt, "游戏豆发布列表");
        }

        /// <summary>
        /// 游戏豆库存
        /// </summary>
        /// <returns></returns>
        public ActionResult GameRepertory()
        {
            return View();
        }

        public ActionResult GameRepertoryPage(int pageIndex, int pageSize)
        {
            var list = DbContext.GameRepertory.OrderByDescending(o => o.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(list.TotalCount, this.ViewHtml(list));
        }

        //会员出局奖金管理
        public ActionResult MemberOutBonus()
        {
            return View();
        }
        public ActionResult MemberOutBonusPage(int pageIndex, int pageSize)
        {
            var list = DbContext.MemberOutBonus.OrderByDescending(o => o.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(list.TotalCount, this.ViewHtml(list));
        }

        public ActionResult BeanPrice()
        {
            var model = this.DbContext.GameBean.OrderBy(item => item.CreateTime).ToArray()
                .Select(item => new Domain.Model.DTO.KeyValue<string, double>
                {
                    Key = item.CreateTime.ToString("yy/MM/dd HH:mm:ss"),
                    Value = (double)item.Price
                })
                .ToList();
            return View(model);
        }
        [HttpPost]
        public ActionResult BeanPrice(decimal price)
        {
            var model = new GameBean()
            {
                CreateTime = DateTime.Now,
                Price = price
            };
            DbContext.GameBean.Add(model);
            DbContext.SaveChanges();
            return Json(new { state = true, value = "BeanPrice" });
        }
    }
}
